package com.wkbb.sports.controller;

import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.concurrent.RejectedExecutionException;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.RedisKeyConstant;
import com.wkbb.common.handler.JsonUtil;
import com.wkbb.common.utils.ObjectUtil;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.sports.dto.TeachDocumentDto;
import com.wkbb.sports.dto.TeachPlanDetailDto;
import com.wkbb.sports.dto.TeachPlanDto;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.model.HlifeDetect;
import com.wkbb.sports.model.HlifeTeachPlan;
import com.wkbb.sports.model.HlifeTeachPlanDetail;
import com.wkbb.sports.service.TeachPlanDetailService;
import com.wkbb.sports.service.TeachPlanService;
import com.wkbb.sports.utils.BeanBind;
import com.wkbb.sports.utils.DateUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;


@RestController
@Api(description = "教学计划管理")
@RequestMapping("/sports/{apiVersion}/teachplan")
@Slf4j
public class TeachPlanController extends BaseController {

    @Autowired
    private TeachPlanService teachPlanService;
    @Autowired
    private TeachPlanDetailService teachPlanDetailService;
    @Autowired
    BasicFeignService basicFeignService;

    @ApiOperation(value = "查询教学计划")
    @RequestMapping(value = "/selTeachPlan", method = RequestMethod.GET)
    public ResultDTO<?> selTeachPlan(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "年度", required = true) @RequestParam(name = "planYear", required = true) Integer planYear,
            @ApiParam(value = "学校Id", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级Id", required = true) @RequestParam(name = "gradeId", required = true) Long gradeId) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        Map<String, Object> map = new HashMap<>();
        HlifeTeachPlan hlifeTeachPlan;
        try {
            map.put("planYear", planYear);
            map.put("orgId", orgId);
            map.put("gradeId", gradeId);
            hlifeTeachPlan = teachPlanService.getByMapParams(map);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<HlifeTeachPlan>(hlifeTeachPlan);
    }

    @ApiOperation(value = "分页查询教学计划")
    @RequestMapping(value = "/selTeachPlanList", method = RequestMethod.GET)
    public ResultDTO<?> selTeachPlanList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "年度", required = false) @RequestParam(name = "planYear", required = false) Integer planYear,
            @ApiParam(value = "学校Id", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "年级Id", required = false) @RequestParam(name = "gradeId", required = false) Long gradeId,
            @ApiParam(value = "分页数", defaultValue = "1") @RequestParam(name = "pageNo") int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(name = "pageSize") int pageSize) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        Map<String, Object> map = new HashMap<>();
        PageInfo<TeachPlanDto> list;
        try {
            map.put("planYear", planYear);
            if (orgId != null && orgId != 1) {
                map.put("orgId", orgId);
            }
            map.put("gradeId", gradeId);
            list = teachPlanService.selTeachPlanList(map, pageNo, pageSize);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>(list);
    }

    @ApiOperation(value = "删除教学计划")
    @RequestMapping(value = "/delTeachPlan", method = RequestMethod.POST)
    public ResultDTO<?> delTeachPlan(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "ID", required = false) @RequestParam(name = "id", required = false) Long id) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        try {
            if (teachPlanService.delTeachPlan(id) > 0) {
                return new ResultDTO<>();
            }
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
    }

    @ApiOperation(value = "新增教学计划")
    @RequestMapping(value = "/addTeachPlan", method = RequestMethod.POST)
    public ResultDTO<?> addTeachPlan(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "教学计划", required = true) @RequestBody TeachPlanDto teachPlanDto) {

        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        if (checkData(teachPlanDto)) {
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }
        if (teachPlanDto.getTotalCourseCount() > 200) {
            return new ResultDTO<>(ErrorCode.PLAN_OUT_RANGE);
        }
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("planYear", teachPlanDto.getPlanYear());
            map.put("orgId", teachPlanDto.getOrgId());
            map.put("gradeId", teachPlanDto.getGradeId());
            map.put("planType", teachPlanDto.getPlanType());
            if (null != teachPlanService.getByMapParams(map)) {
                return new ResultDTO<>(ErrorCode.TEACH_PLAN_YEAR_EXIST);
            }
            teachPlanDto.setCreateUser(userDto.getName());
            BeanBind bind = new BeanBind(TeachPlanDto.class, HlifeTeachPlan.class);
            HlifeTeachPlan plan = (HlifeTeachPlan) bind.bindBean(teachPlanDto);
            teachPlanService.addTeachPlanForPad(plan);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>();
    }

    @ApiOperation(value = "更新计划明细")
    @RequestMapping(value = "/updTeachPlanDetail", method = RequestMethod.PUT)
    public ResultDTO<?> updTeachPlanDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "计划明细", required = true) @RequestBody TeachPlanDetailDto teachPlanDetailDto) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        if (null == teachPlanDetailDto.getId()) {
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }
        try {
            BeanBind bind = new BeanBind(TeachPlanDetailDto.class, HlifeTeachPlanDetail.class);
            HlifeTeachPlanDetail plandetail = (HlifeTeachPlanDetail) bind.bindBean(teachPlanDetailDto);
            teachPlanDetailService.updTeachPlanDetail(plandetail);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>();
    }

    @ApiOperation(value = "新增计划明细")
    @RequestMapping(value = "/addTeachPlanDetail", method = RequestMethod.POST)
    public ResultDTO<?> addTeachPlanDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "计划明细", required = true) @RequestBody TeachPlanDetailDto teachPlanDetailDto) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        teachPlanDetailDto.setCreateUser(userDto.getName());
        try {
            int seq = teachPlanDetailService.selMaxCourseSeq(teachPlanDetailDto);
            teachPlanDetailDto.setCourseSeq(seq + 1);
            teachPlanDetailDto.setStatus((byte) 1);
            BeanBind bind = new BeanBind(TeachPlanDetailDto.class, HlifeTeachPlanDetail.class);
            HlifeTeachPlanDetail plandetail = (HlifeTeachPlanDetail) bind.bindBean(teachPlanDetailDto);
            teachPlanDetailService.insert(plandetail);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>();
    }


    @ApiOperation(value = "再次授课")
    @RequestMapping(value = "/addTeachPlanDetailForPad", method = RequestMethod.POST)
    public ResultDTO<?> addTeachPlanDetailForPad(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "计划明细", required = true) @RequestBody TeachPlanDetailDto teachPlanDetailDto) {
        HlifeTeachPlanDetail plandetail;
        try {
            int seq = teachPlanDetailService.selMaxCourseSeq(teachPlanDetailDto);
            teachPlanDetailDto.setCourseSeq(seq + 1);
            teachPlanDetailDto.setStatus((byte) 1);
            teachPlanDetailDto.setCourseTime(45);
            BeanBind bind = new BeanBind(TeachPlanDetailDto.class, HlifeTeachPlanDetail.class);
            plandetail = (HlifeTeachPlanDetail) bind.bindBean(teachPlanDetailDto);
            teachPlanDetailService.courseTeachAgain(plandetail);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<HlifeTeachPlanDetail>(plandetail);
    }


    @ApiOperation(value = "删除计划明细")
    @RequestMapping(value = "/delTeachPlanDetail", method = RequestMethod.POST)
    public ResultDTO<?> delTeachPlanDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "明细ID", required = true) @RequestParam(name = "id", required = true) List<Long> ids) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        List<HlifeTeachPlanDetail> list = new ArrayList<>();
        try {
            ids.forEach(id -> {
                HlifeTeachPlanDetail hlifeTeachPlanDetail = new HlifeTeachPlanDetail();
                hlifeTeachPlanDetail.setId(id);
                list.add(hlifeTeachPlanDetail);
            });
            teachPlanDetailService.deleteBatch(list);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<HlifeDetect>();
    }

    @ApiOperation(value = "发布计划明细")
    @RequestMapping(value = "/publishPlanDetail", method = RequestMethod.GET)
    public ResultDTO<?> publishPlanDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "计划明细ID", required = true) @RequestParam(name = "teachPlanDetailId", required = true) Long teachPlanDetailId
    ) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        try {
            teachPlanDetailService.publishPlanDetail(teachPlanDetailId);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<HlifeDetect>();
    }


    @ApiOperation(value = "查询班级计划明细列表")
    @RequestMapping(value = "/selClassPlanDetail", method = RequestMethod.GET)
    public ResultDTO<?> selClassPlanDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级ID", required = true) @RequestParam(name = "gradeId", required = true) Long gradeId,
            @ApiParam(value = "班级ID", required = true) @RequestParam(name = "classId", required = true) Long classId,
            @ApiParam(value = "学期", required = false) @RequestParam(name = "planTerm", required = false) Long planTerm) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        List<TeachPlanDetailDto> list;
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("orgId", orgId);
            map.put("gradeId", gradeId);
            map.put("classId", classId);
            Byte clssType = basicFeignService.getClassInfo(orgId, classId).getData().getClassType();
            map.put("planType", clssType);
            if (null == planTerm) {
                map.put("planTerm", DateUtil.getTerm());
            } else {
                map.put("planTerm", planTerm);
            }
            int planYear;
            if (Integer.parseInt(DateUtil.getMonth()) > 8) {
                planYear = Integer.parseInt(DateUtil.getYear()) + 1;
            } else {
                planYear = Integer.parseInt(DateUtil.getYear());
            }
            map.put("planYear", planYear);
            list = teachPlanDetailService.selClassPlanDetail(map);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<List<TeachPlanDetailDto>>(list);
    }

    @ApiOperation(value = "查询计划明细列表")
    @RequestMapping(value = "/selPlanDetail", method = RequestMethod.GET)
    public ResultDTO<?> selPlanDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "计划ID", required = true) @RequestParam(name = "teachingPlanId", required = true) Long teachingPlanId,
            @ApiParam(value = "学期", required = false) @RequestParam(name = "planTerm", required = false) String planTerm,
            @ApiParam(value = "项目", required = false) @RequestParam(name = "planProject", required = false) String planProject,
            @ApiParam(value = "分页数", defaultValue = "1") @RequestParam(name = "pageNo") int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(name = "pageSize") int pageSize
    ) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        PageInfo<TeachPlanDetailDto> list;

        try {
            Map<String, Object> map = new HashMap<>();

            map.put("teachingPlanId", teachingPlanId);
            map.put("planTerm", planTerm);
            map.put("planProject", planProject);
            list = teachPlanDetailService.selPlanDetail(map, pageNo, pageSize);

        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>(list);
    }


    @ApiOperation(value = "添加计划教案")
    @RequestMapping(value = "/addTeachDocument", method = RequestMethod.POST)
    public ResultDTO<?> addTeachDocument(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "教案", required = true) @RequestBody TeachDocumentDto teachDocumentDto) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }

        try {

            teachPlanDetailService.addDocument(teachDocumentDto);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>();
    }

    @ApiOperation(value = "删除计划教案")
    @RequestMapping(value = "/delTeachDocument", method = RequestMethod.POST)
    public ResultDTO<?> addTeachDocument(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "ID", required = false) @RequestParam(name = "id", required = false) Long id) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        try {
            teachPlanDetailService.delTeachDocument(id);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>();
    }


    @ApiOperation(value = "编辑计划教案")
    @RequestMapping(value = "/updTeachDocument", method = RequestMethod.PUT)
    public ResultDTO<?> updTeachDocument(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "教案", required = true) @RequestBody TeachDocumentDto teachDocumentDto) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        if (teachDocumentDto.getId() == null) {
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }
        try {
            teachPlanDetailService.updDocument(teachDocumentDto);
        } catch (Throwable e) {
            logError(e);
            return new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
        }
        return new ResultDTO<>();
    }


    @ApiOperation(value = "查询计划教案")
    @RequestMapping(value = "/selTeachDocument", method = RequestMethod.GET)
    public ResultDTO<?> selTeachDocument(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "教案ID列表", required = true) @RequestParam(name = "documentIds", required = true) String documentIds) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        List<TeachDocumentDto> dtos = teachPlanDetailService.selDocument(documentIds);
        return new ResultDTO<>(dtos);
    }


    @ApiOperation(value = "分页查询计划教案")
    @RequestMapping(value = "/selTeachDocumentList", method = RequestMethod.GET)
    public ResultDTO<?> selTeachDocumentList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校id", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "项目id", required = false) @RequestParam(name = "docProject", required = false) String docProject,
            @ApiParam(value = "标签", required = false) @RequestParam(name = "docTag", required = false) String docTag,
            @ApiParam(value = "年级", required = false) @RequestParam(name = "gradeId", required = false) String gradeId,
//			@ApiParam(value = "年级", required = false) @RequestParam(name = "gradeName", required = false) String gradeName,
            @ApiParam(value = "分页数", defaultValue = "1") @RequestParam(name = "pageNo") int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(name = "pageSize") int pageSize) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }

        Map<String, Object> map = new HashMap<>();
        if (orgId != null && orgId != 1) {
            map.put("orgId", orgId);
        }
        map.put("docProject", docProject);
        map.put("docTag", docTag);
//		map.put("gradeName", gradeName);
        map.put("gradeId", gradeId);
        PageInfo<TeachDocumentDto> dtos = null;

        try {
            dtos = teachPlanDetailService.selTeachDocumentList(map, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(dtos);
    }

    @ApiOperation(value = "计划关联教案列表")
    @RequestMapping(value = "/bindDocuments", method = RequestMethod.POST)
    public ResultDTO<?> bindDocuments(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "计划明细ID", required = true) @RequestParam(name = "teachPlanDetailId", required = true) Long teachPlanDetailId,
            @ApiParam(value = "教案ID列表", required = true) @RequestParam(name = "documentIds", required = true) String documentIds) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        boolean b = teachPlanDetailService.bindDocuments(teachPlanDetailId, documentIds);
        return new ResultDTO<>(b);
    }

    @ApiOperation(value = "计划关联教学资源列表")
    @RequestMapping(value = "/bindFiles", method = RequestMethod.POST)
    public ResultDTO<?> bindFiles(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "计划明细ID", required = true) @RequestParam(name = "teachPlanDetailId", required = true) Long teachPlanDetailId,
            @ApiParam(value = "教学资源ID列表", required = true) @RequestParam(name = "fileIds", required = true) String fileIds) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        boolean b = teachPlanDetailService.bindFiles(teachPlanDetailId, fileIds);
        return new ResultDTO<>(b);
    }

    @ApiOperation(value = "教案批量导入", notes = "")
    @PostMapping(value = "putExcelDoc")
    public ResultDTO<String> putExcelStu(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @RequestParam("uploadFile") MultipartFile uploadFile) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        if (null == uploadFile) {
            return new ResultDTO(ErrorCode.PARAM_MISSING);
        }
        InputStream in = null;
        try {
            in = uploadFile.getInputStream();
            List<Object> excelData = EasyExcel.read(in).sheet().doReadSync();
            List<TeachDocumentDto> list = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            for (Object object : excelData) {
                Map<Integer, String> map = (Map<Integer, String>) object;
                TeachDocumentDto teachDocumentDto = new TeachDocumentDto();
                String docTag = map.get(0).trim();
                teachDocumentDto.setOrgId(orgId);
                teachDocumentDto.setDocTag(docTag.replaceAll("，", ","));
                teachDocumentDto.setOutline(map.get(1).trim());
                teachDocumentDto.setTeachAim(map.get(2));
                teachDocumentDto.setTeachWarm(map.get(3));
                teachDocumentDto.setTeachContent(map.get(4));
//				teachDocumentDto.setTeachSummary(map.get(5).trim());
                String gradeIds = map.get(5);
                if (gradeIds != null) {
                    teachDocumentDto.setGradeIds(gradeIds.trim().replaceAll("，", ","));
                    list.add(teachDocumentDto);
                }
                sb.append(teachDocumentDto.getOutline()).append(",");
            }
            log.info("putExcelDoc:" + sb);
            int count = teachPlanService.putExcelDoc(orgId, list);
            //被强制取消，处理完所有线程后，再返回给用户界面提示异常
            return new ResultDTO<>("成功导入教案" + count + "条");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return new ResultDTO<>(false);
    }

    private boolean checkData(TeachPlanDto teachPlanDto) {
        return teachPlanDto == null || teachPlanDto.getOrgId() == null || teachPlanDto.getGradeId() == null
                || teachPlanDto.getPlanYear() == null ||
                teachPlanDto.getTotalCourseCount() == null || teachPlanDto.getFirstCourseCount() == null
                || teachPlanDto.getSecondCourseCount() == null;

    }

    private void logError(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        log.error(sw.toString());

    }

}
